package com.zyh.scs.admin.utils.jd;

import com.alibaba.fastjson.JSON;
import com.zyh.scs.admin.base.jd.QueryRefundParam;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * desc: 参数加密解密
 *
 * @author hwh
 */
@Slf4j
public class QueryRefundParamUtil {

    /**
     * 参数加密
     *
     * @param srcStr           要加密的原始字符串
     * @param rsaEncryptPubKey 加密RSA公钥
     * @return
     */
    public static String encryptParam(String srcStr, String rsaEncryptPubKey) {
        try {
            byte[] s1Byte = RsaCoder.encryptByPublicKey(srcStr.getBytes(), rsaEncryptPubKey);
            return RsaCoder.encryptBASE64(s1Byte);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 参数解密
     *
     * @param encryptedStr   已加密的字符串
     * @param rsaSignPrivKey 加密RSA私钥
     * @return
     */
    public static String decryptParam(String encryptedStr, String rsaSignPrivKey) {
        try {
            byte[] s1EncryptedByte = RsaCoder.decryptBASE64(encryptedStr);
            byte[] s1Byte = RsaCoder.decryptByPrivateKey(s1EncryptedByte, rsaSignPrivKey);
            return new String(s1Byte);
        } catch (Exception e) {
            System.out.println("参数解密失败,encryptedStr=" + encryptedStr);
            e.printStackTrace();
            return null;
        }
    }


    public static QueryRefundParam queryRefundOrderParam(String rsaSignPrivateKey, String rsaEncryptPublicKey,
                                                         String accessKey, String startDate, String endDate, String pageSize, String pageNum) {

        QueryRefundParam queryRefundParam = new QueryRefundParam();
        queryRefundParam.setAccessKey(accessKey);
        // Long timestamp = System.currentTimeMillis() - 15;
        Long timestamp = System.currentTimeMillis() / 1000 - 18;
        queryRefundParam.setTimestamp(timestamp + "");
        queryRefundParam.setPageSize(pageSize);
        queryRefundParam.setPageNum(pageNum);
        queryRefundParam.setStartDate(startDate);
        queryRefundParam.setEndDate(endDate);

        //sign验证 //这两个参数不参与签名
        List<String> unsignedKeyList = new ArrayList<>();
        unsignedKeyList.add("version");
        unsignedKeyList.add("sign");
        // 获取验签sign
        // 1.将参数按照ASCII码从小到大的顺序排列并使用&符号拼接-s1
        String s1 = null;
        try {
            s1 = SignatureUtil.signString(queryRefundParam, unsignedKeyList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 2.将s1使用"SHA-256"加密方式进行加密获取字符串-s2
        String s2 = ShaUtil.encrypt(s1, "SHA-256");
        // 3.将s2再使用验签私钥进行加密成为s3
        assert s2 != null;
        byte[] s2Rsa = null;
        try {
            s2Rsa = RsaCoder.encryptByPrivateKey(s2.getBytes(StandardCharsets.UTF_8), rsaSignPrivateKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 4.将s3进行base64进行编码
        String sign = null;
        try {
            sign = RsaCoder.encryptBASE64(s2Rsa);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        queryRefundParam.setSign(sign);
        log.info("查询参数{}", JSON.toJSONString(queryRefundParam));
        return queryRefundParam;
    }

}
